﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using zUfDsl.Common;
using zUfDsl.Models;
using zUfDsl.ModelUF;
using zUfDsl.Sync.Post;

namespace zUfDsl.Sync
{
    public partial class FrmInventoryClass : Form
    {
        #region 系统初始化
        private delegate void SetVal();
        private delegate string GetVal(Control c);
        private zUfDsl.Models.tipConfig _tipconfig;
        private Common.HandleDataGrid _handleDataGrid;
        private delegate List<T> GetValList<T>(DataGridView c);
        public FrmInventoryClass(string cInvCCodeSel)
        {
            _handleDataGrid = new Common.HandleDataGrid();
            this.Icon = Properties.Resources.Fly1;
            this.Text = "存货分类选择";
            InitializeComponent();
            this.tbGrade1.Text = cRegistry.readKey("tbGrade1", "InventoryClass", "");
            this.tbGrade2.Text = cRegistry.readKey("tbGrade2", "InventoryClass", "");
            using (var db = new Common.HandleDb().GetUfData())
            {
                InitGrid(db, cInvCCodeSel);
            }
        }


        #region 初始化Grid
        private void InitGrid(U8DataContext db, string cInvCCodeSel)
        {
            #region 初始化格式
            var lcolset = new List<reportColumns>();

            var tmpcolindex = new reportColumns() { Code = "rowindex", Title = "行号", Align = 2, Width = 60, bModify = false, iOrder = 1 };
            lcolset.Add(tmpcolindex);
            //tmpcolindex = new reportColumns() { Code = "rowindex", Title = "行号", Align = 2, Width = 60, bModify = false, iOrder = 1 };
            //lcolset.Add(tmpcolindex);
            var tmpcball = new reportColumns() { Code = "cball", Title = "cball", Align = 2, Width = 50, bModify = true, iOrder = 3,iSort=1, };
            lcolset.Add(tmpcball);
            var tmpcol = new reportColumns() { Code = "cInvCCode", Title = "存货分类编码", Align = 1, Width = 200, bModify = false, iOrder = 4 ,iSort = 1, };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "cInvCName", Title = "存货分类名称", Align = 1, Width = 300, bModify = false, iOrder = 5, iSort = 1, };
            lcolset.Add(tmpcol);
            _handleDataGrid.InitGrid(this.superGridSum1, lcolset);
            this.superGridSum1.Sorted += _handleDataGrid.dtvSorted;
            #endregion
            #region 初始化数据
            var igrade1 = this.tbGrade1.Text.ObjToInt();
            var igrade2 = this.tbGrade2.Text.ObjToInt();
            if (igrade2 <= 0) { igrade2 = 10; }
            var l_invc = db.InventoryClass.Where(s=>s.iInvCGrade>=igrade1 && s.iInvCGrade<=igrade2)
                .Select(s => new invnetoryClassSel { cInvCCode = s.cInvCCode, cInvCName = s.cInvCName })
                .OrderBy(s => s.cInvCCode)
                .ToList();
            _handleDataGrid.BlindData(l_invc);
            var split_cinvccode = cInvCCodeSel.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < this.superGridSum1.Rows.Count; i++)
            {
                var trow = this.superGridSum1.Rows[i];
                var cInvCCode = trow.Cells["cInvCCode"].Value.ToString();
                var tmpjud = split_cinvccode.FirstOrDefault(s => s == cInvCCode);
                if (tmpjud != null)
                {
                    trow.Cells["cball"].Value = true;
                }
            }
            _handleDataGrid.AddRowIndex(this.superGridSum1);
            #endregion
        }
        #endregion
        #endregion

        #region 获取选择的值
        /// <summary>
        /// 获取选择的值
        /// </summary>
        public string GetSel()
        {
            var currentcell = superGridSum1.CurrentCell;
            int icol = 0;
            for (int j = 0; j < superGridSum1.Columns.Count; j++)
            {
                if (superGridSum1.Columns[j].Visible)
                {
                    icol = j;
                    break;
                }
            }
            var tmp1 = superGridSum1.Rows[0].Cells[icol];
            superGridSum1.CurrentCell = tmp1;
            if (currentcell != null && currentcell.RowIndex>=0 )
            {
                superGridSum1.CurrentCell = currentcell;
            }

            var lsel = new List<string>();
            var bselall = true;
            var bselnone = true;
            for (int i = 0; i < this.superGridSum1.Rows.Count; i++)
            {
                var trow = this.superGridSum1.Rows[i];
                if (trow.Cells["cball"].Value.ObjToBool() == true)
                {
                    lsel.Add(trow.Cells["cInvCCode"].Value.ObjToString());
                    bselnone = false;
                }
                else
                {
                    bselall = false;
                }
            }
            if (bselall || bselnone) { return "全部"; }
            else { return string.Join("|", lsel.ToArray()); }
        }


        #endregion

        #region 全选
        private void tsAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.superGridSum1.Rows.Count; i++)
            {
                var trow = this.superGridSum1.Rows[i];
                trow.Cells["cball"].Value = true;
            }
        }
        #endregion
        #region 不选
        private void tsUnSel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.superGridSum1.Rows.Count; i++)
            {
                var trow = this.superGridSum1.Rows[i];
                trow.Cells["cball"].Value = false;
            }
        }
        #endregion
        private bool _bCancel = false;
        #region 确定
        private void tsOK_Click(object sender, EventArgs e)
        {
            _bCancel = false ;
            this.Hide();
        }
        #endregion

        private void tsCancel_Click(object sender, EventArgs e)
        {
            _bCancel = true;
            this.Hide();

        }

        private void FrmInventoryClass_Resize(object sender, EventArgs e)
        {
            if(this.Width>100)
            {
                this.superGridSum1.Width = this.Width - 50;
            }
            if(this.Height>150)
            {
                this.superGridSum1.Height = this.Height - 140;
            }
        }

        private void tsSearch_Click(object sender, EventArgs e)
        {
            cRegistry.saveKey("tbGrade1", this.tbGrade1.Text.Trim(), "InventoryClass");
            cRegistry.saveKey("tbGrade2", this.tbGrade2.Text.Trim(), "InventoryClass");
            var sel = GetSel();
            using (var db = new Common.HandleDb().GetUfData())
            {
                InitGrid(db, sel);
            }
        }
    }
}
